import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'

Vue.config.productionTip = false

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 8,
    list: [2, 1, 3, 4, 6, 5],
    a: 'a',
    b: 'b'
  },
  mutations: {
    addCount (state, payload) {
      state.count += payload
    }
  },
  actions: {
    asyncAddCount (context, payload) {
      setTimeout(() => {
        context.commit('addCount', payload)
      }, 2000) 
    }
  },
  getters: {
    oddList (state) {
      // 数组循环方法 filter map some every forEach
      // return state.list.filter(function (item) {
      //   return item % 2 === 1
      // })
      
      return state.list.filter(item => item % 2 === 1)
    },
    userUsername (state) {
      return state.user.username
    },
    settingAddress (state) {
      return state.setting.address
    }
  },
  modules: {
    user: {
      namespaced: true,
      state: {
        username: '张三'
      },
      mutations: {
        changeUsername (state) {
          state.username += '!' 
        }
      }
    },
    setting: {
      state: {
        address: '合肥'
      }
    }
  }
})

new Vue({
  render: h => h(App),
  store
}).$mount('#app')
